package service

import (
	"gin-demo/mapper"
	"gin-demo/pkg/e"
	"gin-demo/utils/bizerror"
	"github.com/astaxie/beego/validation"
	"net/http"
	"sync"
)

type BaseService struct {
	Mutex *sync.Mutex
}

//获取基础mapper
func (base BaseService) GetMapper() *mapper.BaseMapper {
	return new(mapper.BaseMapper)
}

func (base BaseService) Lock() {
	if base.Mutex == nil {
		bizcode.BizCode{}.PanicError(http.StatusOK, e.ErrorSystemLock, "")
	}
	base.Mutex.Lock()
}

func (base BaseService) Unlock() {
	if base.Mutex == nil {
		bizcode.BizCode{}.PanicError(http.StatusOK, e.ErrorSystemLock, "")
	}
	base.Mutex.Unlock()
}

func (base BaseService) CheckParam(params interface{}) {
	if params != nil {
		valid := validation.Validation{}
		result, err := valid.Valid(params)
		bizcode.Check(err)
		if !result {
			for _, err := range valid.Errors {
				bizcode.BizCode{}.PanicError(http.StatusOK, e.InvalidParams, err.Field+" "+err.Message)
			}
		}
	}
}
